package com.daftmaster.controller;

import com.daftmaster.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关操作控制层
 *
 * @Author Daft_V
 * @Date 2022/3/21 22:11
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 用户登录
     *
     * @param user_account  账号
     * @param user_password 密码
     * @return 返回json字符串
     */
    @PostMapping("/userLogin")
    @ResponseBody
    public JSONObject Login(String user_account, String user_password) {
        Boolean loginFlag = userService.userLogin(user_account, user_password);
        Map<String, Object> map = new HashMap<>();
        if (loginFlag) {
            map.put("code", 200);
            map.put("href", "http://127.0.0.1:9999/addUser");
            map.put("message", "User Login sucess..");
            JSONObject jsonObject = JSONObject.fromObject(map);
            return jsonObject;
        } else {
            map.put("code", 200);
            map.put("href", "http://127.0.0.1:9999/");
            map.put("message", "User Login failed..");
            JSONObject jsonObject = JSONObject.fromObject(map);
            return jsonObject;
        }
    }

    /**
     * 添加用户接口
     *
     * @param userName     用户名
     * @param userAccount  账号
     * @param userPassword 密码
     * @param phone        手机号
     * @param email        邮箱
     * @return 返回json字符串
     */
    @PostMapping("/addUser")
    @ResponseBody
    public JSONObject addUser(String userName, String userAccount, String userPassword, String phone, String email) {
        return userService.userRegister(userName, userAccount, userPassword, phone, email);
    }
}
